﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using V5_Common;
using V5_Data.Model;
using System.Xml;
using System.IO;
using System.Data;

namespace V5_Data {
    public class V5TemplateHelper {
        private static readonly V5TemplateHelper m_Instance = new V5TemplateHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5TemplateHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 清除模板泛型列表
        /// </summary>
        public void ClearCache() {
            DataCacheHelper.RemoveAllCache("Template-Rewrite");
        }
        /// <summary>
        /// 获取模板地址列表
        /// </summary>
        /// <param name="templatePath"></param>
        /// <returns></returns>
        public List<ModelTemplateView> GetLoadAllUrl(string templatePath) {
            List<ModelTemplateView> listUrl = new List<ModelTemplateView>();
            object cache = DataCacheHelper.GetCache("Template-Rewrite");
            if (cache != null) {
                listUrl = (List<ModelTemplateView>)cache;
            }
            else {
                try {
                    string path = string.Empty;
                    path = HttpContext.Current.Server.MapPath("~/Template/") + templatePath + "/settings.config";
                    XmlDocument xml = new XmlDocument();
                    xml.Load(path);
                    //获取所有Url链接
                    XmlNodeList urlNodes = xml.SelectNodes("templatesettings/urls/url");
                    if (urlNodes != null) {
                        ModelTemplateView url = null;
                        XmlNodeList childNodes = null;
                        foreach (XmlNode urlNode in urlNodes) {
                            url = new ModelTemplateView();
                            childNodes = urlNode.ChildNodes;
                            foreach (XmlNode node in childNodes) {
                                //节点名称
                                switch (node.Name.ToLower()) {
                                    case "title":
                                        url.Title = node.InnerText;
                                        break;
                                    case "type":
                                        url.Type = node.InnerText;
                                        break;
                                    case "lookfor":
                                        url.Lookfor = node.InnerText;
                                        break;
                                    case "sendurl":
                                        url.SendUrl = node.InnerText;
                                        break;
                                    case "rewriteurl":
                                        url.RewriteUrl = node.InnerText;
                                        break;
                                    case "htmlurl":
                                        url.HtmlUrl = node.InnerText;
                                        break;
                                    case "issystem":
                                        url.IsSystem = node.InnerText;
                                        break;
                                }
                            }
                            listUrl.Add(url);
                        }

                    }
                }
                catch (Exception ex) {
                    //LoggerHelper.Write(LogLevel.Error, ex);
                }
                DataCacheHelper.SetCache("Template-Rewrite", listUrl);
            }
            return listUrl;
        }
        /// <summary>
        /// 获取模板文件信息
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public List<ModelTemplate.TemplateInfo> GetAllTemplateInfo(string filePath) {
            string templatePath = string.Empty;
            List<ModelTemplate.TemplateInfo> listtempInfo = new List<ModelTemplate.TemplateInfo>();
            DirectoryInfo[] directories = new DirectoryInfo(filePath).GetDirectories();
            ModelTemplate.TemplateInfo tpl = null;
            for (int i = 0; i < directories.Length; i++) {
                try {
                    tpl = new ModelTemplate.TemplateInfo();
                    templatePath = directories[i].Name.ToLower();
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(filePath + templatePath + "/settings.config");
                    XmlNodeList DocdNodeList = xmldoc.DocumentElement.ChildNodes;
                    tpl.TemplateDir = templatePath;
                    foreach (XmlNode node in DocdNodeList) {
                        switch (node.Name.ToLower()) {
                            case "name":
                                tpl.Name = node.InnerText;
                                break;
                            case "image":
                                tpl.Image = node.InnerText;
                                break;
                            case "description":
                                tpl.Description = node.InnerText;
                                break;
                            case "version":
                                tpl.Version = node.InnerText;
                                break;
                            case "time":
                                tpl.Time = node.InnerText;
                                break;
                            case "author":
                                tpl.Author = node.InnerText;
                                break;
                        }
                    }
                    listtempInfo.Add(tpl);
                }
                catch {
                    continue;
                }
            }
            return listtempInfo;
        }

        #region TempateView
        public List<ModelTemplateView> ListLoadSystemViewUrls() {
            List<ModelTemplateView> ListUrlHelper = new List<ModelTemplateView>();
            object cache = DataCacheHelper.GetCache("System-Template-Rewrite");
            if (cache != null) {
                ListUrlHelper = (List<ModelTemplateView>)cache;
            }
            else {
                #region 系统自带
                string xmlSystemPath = HttpContext.Current.Server.MapPath("~/Config/") + "V5Urls.config";
                try {
                    DataSet dsSystem = new DataSet();
                    dsSystem.ReadXml(xmlSystemPath);
                    if (dsSystem.Tables.Count > 0) {
                        DataTable dt = dsSystem.Tables[0];
                        foreach (DataRow dr in dt.Rows) {
                            ModelTemplateView model = new ModelTemplateView();
                            model.Title = "系统-" + dr["Title"].ToString();
                            model.Lookfor = dr["Lookfor"].ToString();
                            model.Type = dr["Type"].ToString();
                            model.SendUrl = dr["SendUrl"].ToString();
                            model.RewriteUrl = dr["RewriteUrl"].ToString();
                            model.HtmlUrl = dr["HtmlUrl"].ToString();
                            model.IsSystem = dr["IsSystem"].ToString();
                            model.LookforParams = dr["lookforparams"].ToString();
                            ListUrlHelper.Add(model);
                        }
                        DataCacheHelper.SetCache("System-Template-Rewrite", ListUrlHelper);
                    }
                }
                catch (Exception ex) {
                    //LoggerHelper.Write(LogLevel.Error, ex);
                }
                #endregion
            }
            return ListUrlHelper;
        }

        public List<ModelTemplateView> ListLoadAllViewUrls(string TemplateDir) {
            List<ModelTemplateView> ListUrlHelper = new List<ModelTemplateView>();
            ListUrlHelper.AddRange(ListLoadSystemViewUrls());
            ListUrlHelper.AddRange(GetLoadAllUrl(TemplateDir));
            return ListUrlHelper;
        }


        /// <summary>
        /// 根据Control获取模板显示模型
        /// </summary>
        /// <param name="strLookFor"></param>
        /// <returns></returns>
        public ModelTemplateView GetTemplateViewModel(string TemplateDir, string strLookFor) {
            List<ModelTemplateView> list = ListLoadAllViewUrls(TemplateDir);
            ModelTemplateView model = list.Where(p => p.Lookfor == strLookFor).FirstOrDefault();
            if (model != null) {
                return model;
            }
            return null;
        }
        #endregion
    }
}